package com.lb.shopguide.http.interceptor;

import android.text.TextUtils;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.LogUtils;
import com.lb.shopguide.GuideApplication;
import com.lb.shopguide.config.UserConfigManager;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class ParamInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        HttpUrl url = original.url().newBuilder().addQueryParameter("deviceId", DeviceUtils.getAndroidID()).build(); //添加请求头
        String userToken = UserConfigManager.getInstance(GuideApplication.context()).getUserToken();
        Request request = original;
        if (!TextUtils.isEmpty(userToken)) {
            LogUtils.d("Authorization====" + userToken);
            //测试使用
//            userToken = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFM1MTIifQ.eyJ1c2VyQ29kZSI6IjA4ZmQ1ZDQ2NzA3ZjQzZTQ5ZmVhZjdjYzU4ZjY5MzY1IiwiaXNzIjoid2x3YkFwaSIsImF1ZCI6IjdhMTRlNzAxNWQwYTQ5YzZhMzA4Yjk4ZTQwMGYyOWQ4In0.r2lYYysTOrzO7hYPfpnJqEE6g_nUbDUNq8OlWxWsj5aV_8giME16_TVdbkQRMRrV26TvApBipLUhWG9s1H4fxw";
            request = original.newBuilder().addHeader("Authorization", userToken).method(original.method(), original.body()).url(url.url()).build();
        }

        return chain.proceed(request);
    }
}
